package com.qy.controller.app;

import com.qy.base.core.*;
import com.qy.base.utils.DateUtil;
import com.qy.model.Charge;
import com.qy.model.Evaluation;
import com.qy.model.Policy;
import com.qy.model.Unit;
import com.qy.service.ChargeService;
import com.qy.service.EvaluationService;
import com.github.pagehelper.PageHelper;
import com.qy.service.PolicyService;
import com.qy.service.UnitService;
import org.apache.ibatis.jdbc.Null;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by zaq on 2018/07/10.
 */
@RestController
@RequestMapping("app/evaluation")
public class EvaluationController {
    @Resource
    private EvaluationService evaluationService;

    @Resource
    private PolicyService policyService;

    @Resource
    private UnitService unitService;

    @Resource
    private ChargeService chargeService;

    @PostMapping("/add")
    public Result add(@RequestBody Evaluation evaluation) {
        evaluationService.save(evaluation);
        return ResultGenerator.successResult();
    }

    //删除
    @PostMapping("/delete")
    public Result delete(@RequestBody Evaluation evaluation) {
        if (evaluation.getId() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        evaluationService.deleteById(evaluation.getId());
        return ResultGenerator.successResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Evaluation evaluation) {
        evaluationService.update(evaluation);
        return ResultGenerator.successResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Evaluation evaluation = evaluationService.findById(id);
        return ResultGenerator.successResult(evaluation);
    }

    @GetMapping("/list")
    public Result list(PageBean<Evaluation> page) {
        PageHelper.startPage(page.getPageNum(), page.getSize());
        List<Evaluation> list = evaluationService.findAll();
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //判断是否测评过
    @GetMapping("/checkEvaluation")
    public Result checkEvaluation(Policy policy) {
        if (policy.getId() == null || policy.getUser_id() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        List<Evaluation> list = new ArrayList<>();
        List<Evaluation> evaluationList = evaluationService.selectEvaluationByUserId(policy);
        if (evaluationList.size() != 0) {
            for (int i = 0; i < evaluationList.size(); i++) {
                Evaluation evaluation = evaluationList.get(i);
                if (evaluation.getPolicy_ids() != null && !evaluation.getPolicy_ids().isEmpty()) {
                    if (evaluation.getPolicy_ids().contains(policy.getId().toString())) {
                        list.add(evaluation);
                    }
                }
            }
        }
        return ResultGenerator.successResult(list);
    }

    //政策测评
    @PostMapping("/evaluation")
    public Result evaluation(@RequestBody Evaluation evaluation) {
        if (evaluation.getPageNum() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        }
        Integer pageNum = evaluation.getPageNum();
        if (evaluation.getId() != null) {
            evaluation = evaluationService.findById(evaluation.getId());
        }
        if (evaluation.getUser_id() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        evaluation.setStartNum((pageNum - 1) * 10);
        if (evaluation.getPolicy_type() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        }
        if (evaluation.getProvinces() == null || evaluation.getProvinces().isEmpty()) {
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        }
        if (evaluation.getPolicy_need() == null || evaluation.getPolicy_need().isEmpty()) {
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        }

        String replaceAll1 = evaluation.getProvinces().replaceAll("\"", "").replaceAll("\\[", "").replaceAll("]", "");
        evaluation.setProvinces(replaceAll1);

        String replaceAll2 = evaluation.getPolicy_need().replaceAll("\"", "").replaceAll("\\[", "").replaceAll("]", "");
        evaluation.setPolicy_need(replaceAll2);

        if (evaluation.getProvinces().contains(",")) {
            String[] provincess = evaluation.getProvinces().split(",");
            evaluation.setProvincess(provincess);
        } else {
            String[] provincess = new String[1];
            provincess[0] = evaluation.getProvinces();
            evaluation.setProvincess(provincess);
        }

        if (evaluation.getPolicy_need().contains(",")) {
            String[] policyNeeds = evaluation.getPolicy_need().split(",");
            evaluation.setPolicyNeeds(policyNeeds);
        } else {
            String[] policyNeeds = new String[1];
            policyNeeds[0] = evaluation.getPolicy_need();
            evaluation.setPolicyNeeds(policyNeeds);
        }

        Map<String, Object> map = new HashMap<>();

        //不同类型的匹配规则不同
        if (evaluation.getPolicy_type() == 1) {
            List<Map> list1 = policyService.selectFirstPolicyListByEvaluation1(evaluation);
            if (list1.size() == 0) {
                List<Map> list2 = policyService.selectSecondPolicyListByEvaluation1(evaluation);
                if (list2.size() == 0) {
                    List<Map> list3 = policyService.selectThirdPolicyListByEvaluation1(evaluation);
                    if (list3.size() == 0) {
                        List<Map> list4 = policyService.selectFourthPolicyListByEvaluation1(evaluation);
                        if (list4.size() == 0) {
                            List<Unit> list5 = unitService.selectUnitListByProvince(evaluation);
                            map.put("policyList", null);
                            if (list5.size() == 0) {
                                map.put("unitList", null);
                            } else {
                                map.put("unitList", list5);
                            }
                            map.put("chargeList", null);
                        } else {
                            List<String> policyIdsList = new ArrayList<>();
                            for (int i = 0; i < list4.size(); i++) {
                                policyIdsList.add(list4.get(i).get("id").toString());
                            }
                            evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                            map = evaluationService.selectUnitListAndChargeList(list4);
                        }
                    } else {
                        List<String> policyIdsList = new ArrayList<>();
                        for (int i = 0; i < list3.size(); i++) {
                            policyIdsList.add(list3.get(i).get("id").toString());
                        }
                        evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                        map = evaluationService.selectUnitListAndChargeList(list3);
                    }
                } else {
                    List<String> policyIdsList = new ArrayList<>();
                    for (int i = 0; i < list2.size(); i++) {
                        policyIdsList.add(list2.get(i).get("id").toString());
                    }
                    evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                    map = evaluationService.selectUnitListAndChargeList(list2);
                }
            } else {
                List<String> policyIdsList = new ArrayList<>();
                for (int i = 0; i < list1.size(); i++) {
                    policyIdsList.add(list1.get(i).get("id").toString());
                }
                evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                map = evaluationService.selectUnitListAndChargeList(list1);
            }
        } else if (evaluation.getPolicy_type() == 2) {
            List<Map> list1 = policyService.selectFirstPolicyListByEvaluation2(evaluation);
            if (list1.size() == 0) {
                List<Map> list2 = policyService.selectSecondPolicyListByEvaluation2(evaluation);
                if (list2.size() == 0) {
                    List<Map> list3 = policyService.selectThirdPolicyListByEvaluation2(evaluation);
                    if (list3.size() == 0) {
                        List<Map> list4 = policyService.selectFourthPolicyListByEvaluation2(evaluation);
                        if (list4.size() == 0) {
                            List<Unit> list5 = unitService.selectUnitListByProvince(evaluation);
                            map.put("policyList", null);
                            if (list5.size() == 0) {
                                map.put("unitList", null);
                            } else {
                                map.put("unitList", list5);
                            }
                            map.put("chargeList", null);
                        } else {
                            List<String> policyIdsList = new ArrayList<>();
                            for (int i = 0; i < list4.size(); i++) {
                                policyIdsList.add(list4.get(i).get("id").toString());
                            }
                            evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                            map = evaluationService.selectUnitListAndChargeList(list4);
                        }
                    } else {
                        List<String> policyIdsList = new ArrayList<>();
                        for (int i = 0; i < list3.size(); i++) {
                            policyIdsList.add(list3.get(i).get("id").toString());
                        }
                        evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                        map = evaluationService.selectUnitListAndChargeList(list3);
                    }
                } else {
                    List<String> policyIdsList = new ArrayList<>();
                    for (int i = 0; i < list2.size(); i++) {
                        policyIdsList.add(list2.get(i).get("id").toString());
                    }
                    evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                    map = evaluationService.selectUnitListAndChargeList(list2);
                }
            } else {
                List<String> policyIdsList = new ArrayList<>();
                for (int i = 0; i < list1.size(); i++) {
                    policyIdsList.add(list1.get(i).get("id").toString());
                }
                evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                map = evaluationService.selectUnitListAndChargeList(list1);
            }
        } else if (evaluation.getPolicy_type() == 3) {
            List<Map> list1 = policyService.selectFirstPolicyListByEvaluation3(evaluation);
            if (list1.size() == 0) {
                List<Unit> list5 = unitService.selectUnitListByProvince(evaluation);
                map.put("policyList", null);
                if (list5.size() == 0) {
                    map.put("unitList", null);
                } else {
                    map.put("unitList", list5);
                }
                map.put("chargeList", null);
            } else {
                List<String> policyIdsList = new ArrayList<>();
                for (int i = 0; i < list1.size(); i++) {
                    policyIdsList.add(list1.get(i).get("id").toString());
                }
                evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                map = evaluationService.selectUnitListAndChargeList(list1);
            }
        } else if (evaluation.getPolicy_type() == 4) {
            List<Map> list1 = policyService.selectFirstPolicyListByEvaluation4(evaluation);
            if (list1.size() == 0) {
                List<Map> list2 = policyService.selectSecondPolicyListByEvaluation4(evaluation);
                if (list2.size() == 0) {
                    List<Map> list3 = policyService.selectThirdPolicyListByEvaluation4(evaluation);
                    if (list3.size() == 0) {
                        List<Map> list4 = policyService.selectFourthPolicyListByEvaluation4(evaluation);
                        if (list4.size() == 0) {
                            List<Unit> list5 = unitService.selectUnitListByProvince(evaluation);
                            map.put("policyList", null);
                            if (list5.size() == 0) {
                                map.put("unitList", null);
                            } else {
                                map.put("unitList", list5);
                            }
                            map.put("chargeList", null);
                        } else {
                            List<String> policyIdsList = new ArrayList<>();
                            for (int i = 0; i < list4.size(); i++) {
                                policyIdsList.add(list4.get(i).get("id").toString());
                            }
                            evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                            map = evaluationService.selectUnitListAndChargeList(list4);
                        }
                    } else {
                        List<String> policyIdsList = new ArrayList<>();
                        for (int i = 0; i < list3.size(); i++) {
                            policyIdsList.add(list3.get(i).get("id").toString());
                        }
                        evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                        map = evaluationService.selectUnitListAndChargeList(list3);
                    }
                } else {
                    List<String> policyIdsList = new ArrayList<>();
                    for (int i = 0; i < list2.size(); i++) {
                        policyIdsList.add(list2.get(i).get("id").toString());
                    }
                    evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                    map = evaluationService.selectUnitListAndChargeList(list2);
                }
            } else {
                List<String> policyIdsList = new ArrayList<>();
                for (int i = 0; i < list1.size(); i++) {
                    policyIdsList.add(list1.get(i).get("id").toString());
                }
                evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                map = evaluationService.selectUnitListAndChargeList(list1);
            }
        } else if (evaluation.getPolicy_type() == 5) {
            List<Map> list1 = policyService.selectFirstPolicyListByEvaluation5(evaluation);
            if (list1.size() == 0) {
                List<Map> list2 = policyService.selectSecondPolicyListByEvaluation5(evaluation);
                if (list2.size() == 0) {
                    List<Unit> list5 = unitService.selectUnitListByProvince(evaluation);
                    map.put("policyList", null);
                    if (list5.size() == 0) {
                        map.put("unitList", null);
                    } else {
                        map.put("unitList", list5);
                    }
                    map.put("chargeList", null);
                } else {
                    List<String> policyIdsList = new ArrayList<>();
                    for (int i = 0; i < list2.size(); i++) {
                        policyIdsList.add(list2.get(i).get("id").toString());
                    }
                    evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                    map = evaluationService.selectUnitListAndChargeList(list2);
                }
            } else {
                List<String> policyIdsList = new ArrayList<>();
                for (int i = 0; i < list1.size(); i++) {
                    policyIdsList.add(list1.get(i).get("id").toString());
                }
                evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                map = evaluationService.selectUnitListAndChargeList(list1);
            }
        } else if (evaluation.getPolicy_type() == 6) {
            List<Map> list1 = policyService.selectFirstPolicyListByEvaluation6(evaluation);
            if (list1.size() == 0) {
                List<Unit> list5 = unitService.selectUnitListByProvince(evaluation);
                map.put("policyList", null);
                if (list5.size() == 0) {
                    map.put("unitList", null);
                } else {
                    map.put("unitList", list5);
                }
                map.put("chargeList", null);
            } else {
                List<String> policyIdsList = new ArrayList<>();
                for (int i = 0; i < list1.size(); i++) {
                    policyIdsList.add(list1.get(i).get("id").toString());
                }
                evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                map = evaluationService.selectUnitListAndChargeList(list1);
            }
        }

        evaluation.setAdd_time(DateUtil.getNowTimestamp());
        if (evaluation.getId() == null) {
            evaluationService.save(evaluation);
        } else {
            evaluationService.update(evaluation);
        }

        return ResultGenerator.successResult(map, evaluation.getId());
    }

    //评测记录
    @GetMapping("/evaluationList")
    public Result evaluationList(PageBean<Map> page, Evaluation evaluation) {
        if (evaluation.getUser_id() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        PageHelper.startPage(page.getPageNum(), page.getSize(), "add_time desc");
        List<Map> list = evaluationService.selectEvaluationListByUserId(evaluation);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).put("provinces", list.get(i).get("provinces").toString().replace(",", " "));
            list.get(i).put("policy_need", list.get(i).get("policy_need").toString().replace(",", " "));
        }
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //评测详情
    @GetMapping("/evaluationDetail")
    public Result evaluationDetail(Evaluation evaluation) {
        if (evaluation.getId() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        Evaluation evaluation1 = evaluationService.findById(evaluation.getId());

        Map<String, Object> map = new HashMap<>();
        evaluation1.setProvinces(evaluation1.getProvinces().replace(",", " "));
        evaluation1.setPolicy_need(evaluation1.getPolicy_need().replace(",", " "));
        map.put("evaluation", evaluation1);

        return ResultGenerator.successResult(map);
    }

    //刷新
    @GetMapping("/refresh")
    public Result refresh(Evaluation evaluation) {
        if (evaluation.getPageNum() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        }
        Integer pageNum = evaluation.getPageNum();
        if (evaluation.getId() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        evaluation = evaluationService.findById(evaluation.getId());

        evaluation.setStartNum((pageNum - 1) * 10);

        if (evaluation.getRefresh_time() != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(Long.valueOf(DateUtil.getNowTimestamp()));
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            calendar.set(Calendar.MILLISECOND, 0);
            Long refreshTime = calendar.getTimeInMillis();
            if (Long.valueOf(evaluation.getRefresh_time()) < refreshTime) {
                throw new ServiceException("每天只能刷新一次");
            }
        }

        String replaceAll1 = evaluation.getProvinces().replaceAll("\"", "").replaceAll("\\[", "").replaceAll("]", "");
        evaluation.setProvinces(replaceAll1);

        String replaceAll2 = evaluation.getPolicy_need().replaceAll("\"", "").replaceAll("\\[", "").replaceAll("]", "");
        evaluation.setPolicy_need(replaceAll2);

        if (evaluation.getProvinces().contains(",")) {
            String[] provincess = evaluation.getProvinces().split(",");
            evaluation.setProvincess(provincess);
        } else {
            String[] provincess = new String[1];
            provincess[0] = evaluation.getProvinces();
            evaluation.setProvincess(provincess);
        }

        if (evaluation.getPolicy_need().contains(",")) {
            String[] policyNeeds = evaluation.getPolicy_need().split(",");
            evaluation.setPolicyNeeds(policyNeeds);
        } else {
            String[] policyNeeds = new String[1];
            policyNeeds[0] = evaluation.getPolicy_need();
            evaluation.setPolicyNeeds(policyNeeds);
        }

        Map<String, Object> map = new HashMap<>();

        if (evaluation.getPolicy_type() == 1) {
            List<Map> list1 = policyService.selectFirstPolicyListByEvaluation1(evaluation);
            if (list1.size() == 0) {
                List<Map> list2 = policyService.selectSecondPolicyListByEvaluation1(evaluation);
                if (list2.size() == 0) {
                    List<Map> list3 = policyService.selectThirdPolicyListByEvaluation1(evaluation);
                    if (list3.size() == 0) {
                        List<Map> list4 = policyService.selectFourthPolicyListByEvaluation1(evaluation);
                        if (list4.size() == 0) {
                            List<Unit> list5 = unitService.selectUnitListByProvince(evaluation);
                            map.put("policyList", null);
                            if (list5.size() == 0) {
                                map.put("unitList", null);
                            } else {
                                map.put("unitList", list5);
                            }
                            map.put("chargeList", null);
                        } else {
                            List<String> policyIdsList = new ArrayList<>();
                            for (int i = 0; i < list4.size(); i++) {
                                policyIdsList.add(list4.get(i).get("id").toString());
                            }
                            evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                            map = evaluationService.selectUnitListAndChargeList(list4);
                        }
                    } else {
                        List<String> policyIdsList = new ArrayList<>();
                        for (int i = 0; i < list3.size(); i++) {
                            policyIdsList.add(list3.get(i).get("id").toString());
                        }
                        evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                        map = evaluationService.selectUnitListAndChargeList(list3);
                    }
                } else {
                    List<String> policyIdsList = new ArrayList<>();
                    for (int i = 0; i < list2.size(); i++) {
                        policyIdsList.add(list2.get(i).get("id").toString());
                    }
                    evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                    map = evaluationService.selectUnitListAndChargeList(list2);
                }
            } else {
                List<String> policyIdsList = new ArrayList<>();
                for (int i = 0; i < list1.size(); i++) {
                    policyIdsList.add(list1.get(i).get("id").toString());
                }
                evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                map = evaluationService.selectUnitListAndChargeList(list1);
            }
        } else if (evaluation.getPolicy_type() == 2) {
            List<Map> list1 = policyService.selectFirstPolicyListByEvaluation2(evaluation);
            if (list1.size() == 0) {
                List<Map> list2 = policyService.selectSecondPolicyListByEvaluation2(evaluation);
                if (list2.size() == 0) {
                    List<Map> list3 = policyService.selectThirdPolicyListByEvaluation2(evaluation);
                    if (list3.size() == 0) {
                        List<Map> list4 = policyService.selectFourthPolicyListByEvaluation2(evaluation);
                        if (list4.size() == 0) {
                            List<Unit> list5 = unitService.selectUnitListByProvince(evaluation);
                            map.put("policyList", null);
                            if (list5.size() == 0) {
                                map.put("unitList", null);
                            } else {
                                map.put("unitList", list5);
                            }
                            map.put("chargeList", null);
                        } else {
                            List<String> policyIdsList = new ArrayList<>();
                            for (int i = 0; i < list4.size(); i++) {
                                policyIdsList.add(list4.get(i).get("id").toString());
                            }
                            evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                            map = evaluationService.selectUnitListAndChargeList(list4);
                        }
                    } else {
                        List<String> policyIdsList = new ArrayList<>();
                        for (int i = 0; i < list3.size(); i++) {
                            policyIdsList.add(list3.get(i).get("id").toString());
                        }
                        evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                        map = evaluationService.selectUnitListAndChargeList(list3);
                    }
                } else {
                    List<String> policyIdsList = new ArrayList<>();
                    for (int i = 0; i < list2.size(); i++) {
                        policyIdsList.add(list2.get(i).get("id").toString());
                    }
                    evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                    map = evaluationService.selectUnitListAndChargeList(list2);
                }
            } else {
                List<String> policyIdsList = new ArrayList<>();
                for (int i = 0; i < list1.size(); i++) {
                    policyIdsList.add(list1.get(i).get("id").toString());
                }
                evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                map = evaluationService.selectUnitListAndChargeList(list1);
            }
        } else if (evaluation.getPolicy_type() == 3) {
            List<Map> list1 = policyService.selectFirstPolicyListByEvaluation3(evaluation);
            if (list1.size() == 0) {
                List<Unit> list5 = unitService.selectUnitListByProvince(evaluation);
                map.put("policyList", null);
                if (list5.size() == 0) {
                    map.put("unitList", null);
                } else {
                    map.put("unitList", list5);
                }
                map.put("chargeList", null);
            } else {
                List<String> policyIdsList = new ArrayList<>();
                for (int i = 0; i < list1.size(); i++) {
                    policyIdsList.add(list1.get(i).get("id").toString());
                }
                evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                map = evaluationService.selectUnitListAndChargeList(list1);
            }
        } else if (evaluation.getPolicy_type() == 4) {
            List<Map> list1 = policyService.selectFirstPolicyListByEvaluation4(evaluation);
            if (list1.size() == 0) {
                List<Map> list2 = policyService.selectSecondPolicyListByEvaluation4(evaluation);
                if (list2.size() == 0) {
                    List<Map> list3 = policyService.selectThirdPolicyListByEvaluation4(evaluation);
                    if (list3.size() == 0) {
                        List<Map> list4 = policyService.selectFourthPolicyListByEvaluation4(evaluation);
                        if (list4.size() == 0) {
                            List<Unit> list5 = unitService.selectUnitListByProvince(evaluation);
                            map.put("policyList", null);
                            if (list5.size() == 0) {
                                map.put("unitList", null);
                            } else {
                                map.put("unitList", list5);
                            }
                            map.put("chargeList", null);
                        } else {
                            List<String> policyIdsList = new ArrayList<>();
                            for (int i = 0; i < list4.size(); i++) {
                                policyIdsList.add(list4.get(i).get("id").toString());
                            }
                            evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                            map = evaluationService.selectUnitListAndChargeList(list4);
                        }
                    } else {
                        List<String> policyIdsList = new ArrayList<>();
                        for (int i = 0; i < list3.size(); i++) {
                            policyIdsList.add(list3.get(i).get("id").toString());
                        }
                        evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                        map = evaluationService.selectUnitListAndChargeList(list3);
                    }
                } else {
                    List<String> policyIdsList = new ArrayList<>();
                    for (int i = 0; i < list2.size(); i++) {
                        policyIdsList.add(list2.get(i).get("id").toString());
                    }
                    evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                    map = evaluationService.selectUnitListAndChargeList(list2);
                }
            } else {
                List<String> policyIdsList = new ArrayList<>();
                for (int i = 0; i < list1.size(); i++) {
                    policyIdsList.add(list1.get(i).get("id").toString());
                }
                evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                map = evaluationService.selectUnitListAndChargeList(list1);
            }
        } else if (evaluation.getPolicy_type() == 5) {
            List<Map> list1 = policyService.selectFirstPolicyListByEvaluation5(evaluation);
            if (list1.size() == 0) {
                List<Map> list2 = policyService.selectSecondPolicyListByEvaluation5(evaluation);
                if (list2.size() == 0) {
                    List<Unit> list5 = unitService.selectUnitListByProvince(evaluation);
                    map.put("policyList", null);
                    if (list5.size() == 0) {
                        map.put("unitList", null);
                    } else {
                        map.put("unitList", list5);
                    }
                    map.put("chargeList", null);
                } else {
                    List<String> policyIdsList = new ArrayList<>();
                    for (int i = 0; i < list2.size(); i++) {
                        policyIdsList.add(list2.get(i).get("id").toString());
                    }
                    evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                    map = evaluationService.selectUnitListAndChargeList(list2);
                }
            } else {
                List<String> policyIdsList = new ArrayList<>();
                for (int i = 0; i < list1.size(); i++) {
                    policyIdsList.add(list1.get(i).get("id").toString());
                }
                evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                map = evaluationService.selectUnitListAndChargeList(list1);
            }
        } else if (evaluation.getPolicy_type() == 6) {
            List<Map> list1 = policyService.selectFirstPolicyListByEvaluation6(evaluation);
            if (list1.size() == 0) {
                List<Unit> list5 = unitService.selectUnitListByProvince(evaluation);
                map.put("policyList", null);
                if (list5.size() == 0) {
                    map.put("unitList", null);
                } else {
                    map.put("unitList", list5);
                }
                map.put("chargeList", null);
            } else {
                List<String> policyIdsList = new ArrayList<>();
                for (int i = 0; i < list1.size(); i++) {
                    policyIdsList.add(list1.get(i).get("id").toString());
                }
                evaluation.setPolicy_ids(policyIdsList.toString().replaceAll("\\[", "").replaceAll("]", ""));
                map = evaluationService.selectUnitListAndChargeList(list1);
            }
        }

        evaluation.setRefresh_time(DateUtil.getNowTimestamp());
        evaluation.setAdd_time(DateUtil.getNowTimestamp());
        evaluationService.update(evaluation);

        return ResultGenerator.successResult(map, evaluation.getId());
    }


}
